package com.alstudio.ui.module.account;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.BackgroundColorSpan;
import android.text.style.URLSpan;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.core.iaj.ALIAppJumpManager;
import com.alstudio.module.ALModuleManager;
import com.alstudio.module.xmpp.protocol.xmpp.data.ALXmppData;
import com.alstudio.module.xmpp.request.ALUiXmppRequestCreator;
import com.alstudio.module.xmpp.request.ALUiXmppRequestEvent;
import com.alstudio.module.xmpp.request.ALUiXmppRequestType;
import com.alstudio.pojo.ALXmppEvent;
import com.alstudio.ui.base.TitleBarActivity;
import com.alstudio.utils.android.keyboard.ALKeyBoardManager;
import com.alstudio.utils.formater.ALEmailFormater;
import com.alstudio.utils.log.ALLog;
import com.alstudio.utils.umeng.UmengUtils;
import com.alstudio.view.toast.MyToastUtil;
import com.loovee.imaohu.R;

/**
 * 注册界面
 * 
 * @author alonso lee
 * 
 */
public class RegisterActivity extends TitleBarActivity {

	private EditText etUsername, etConfirmPwd;
	private TextView txt_agree_register_protocol;

	private final String SIMPLE_PWD = "123456";
	private CheckBox agree_box;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setupView(this, getClass().getSimpleName());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#setupView(android.content.Context,
	 * java.lang.String)
	 */
	@Override
	protected void setupView(Context context, String name) {
		// TODO Auto-generated method stub
		super.setupView(context, name);
		setTitleBarContentView(R.layout.register_activity);
		setCenterTextViewMessage(R.string.TxtRegister);
		findViews();
	}

	private void findViews() {
		findViewById(R.id.background).setOnClickListener(this);
		// 各输入框
		etUsername = (EditText) findViewById(R.id.et_account);
		etConfirmPwd = (EditText) findViewById(R.id.et_new_pwd);
		setViewOnclickListener(R.id.next_button, this);
		// 同意条款
		agree_box = (CheckBox) findViewById(R.id.cb_agree_register);
		txt_agree_register_protocol = (TextView) findViewById(R.id.txt_agree_register_protocol);

		agree_box.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					agree_box.setChecked(true);
					agree_box.setButtonDrawable(R.drawable.choose_on);
				} else {
					agree_box.setButtonDrawable(R.drawable.choose_off);
					agree_box.setChecked(false);
				}
			}
		});
		// 不能取消
		setLoadingProgressCancelAble(false);

		showTips();

	}

	private void showTips() {

		ALIAppJumpManager.getIAppJumpManager().setJumpText(
				txt_agree_register_protocol,
				getString(R.string.TxtAgreeRegisterProtocol),
				getResources().getColor(R.color.orange));

	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.background:
			ALKeyBoardManager.dismissKeyBoard(this);
			break;
		case R.id.bt_sina_login:
			doSinaLogin();
			break;
		case R.id.bt_tencent_login:
			doTencentLogin();
			break;
		case R.id.next_button:
			doRegister();
			break;
		}
	}

	/**
	 * 执行注册逻辑
	 */
	private void doRegister() {

		if (!ALLocalEnv.isNetworkConnected()) {
			return;
		}

		if (etUsername.getText().toString().trim().length() == 0) {
			MyToastUtil.getInstance().showToastOnCenter(
					getString(R.string.TxtHintEnterEmail));
			etUsername.requestFocus();
			return;
		}

		// 检测邮箱合法性
		if (!ALEmailFormater.isEmail(etUsername.getText().toString().trim())) {
			MyToastUtil.getInstance().showToastOnCenter(
					getString(R.string.TxtEmailFormatError));
			etUsername.requestFocus();
			return;
		}

		if (etConfirmPwd.getText().toString().trim().length() == 0) {
			// 请输入密码
			MyToastUtil.getInstance().showToastOnCenter(
					getString(R.string.TxtEnterPassword));
			etConfirmPwd.requestFocus();
			return;
		}

		// 密码长度不够
		if (etConfirmPwd.getText().toString().trim().length() < 6) {
			MyToastUtil.getInstance().showToastOnCenter(
					getString(R.string.TxtPasswordTooShort));
			etConfirmPwd.requestFocus();
			return;
		}

		// 未勾选同意条款
		if (!agree_box.isChecked()) {
			MyToastUtil.getInstance().showToastOnCenter(
					getString(R.string.TxtReadProtocol));
			return;
		}

		// 检测是否为简单密码
		String pwd = etConfirmPwd.getText().toString().trim();
		if (pwd.equals(SIMPLE_PWD) || isSimplePwd(pwd)) {
			MyToastUtil.getInstance().showToastOnCenter(
					getString(R.string.TxtPasswordSimple));
			etConfirmPwd.requestFocus();
			return;
		}
		// 发送检测邮箱请求
		ALXmppData data = ALLocalEnv.getLocalEnv().createALXmppData();
		data.setEmail(etUsername.getText().toString().trim());
		ALUiXmppRequestEvent event = ALUiXmppRequestCreator
				.creatorALUiXmppEvent(
						ALUiXmppRequestType.XMPP_CHECK_EMAIL_REQUEST, data);
		ALModuleManager.getDefaultModuleManager().postEventToModuleManager(
				event);

		showLoadingProgress();
		ALKeyBoardManager.dismissKeyBoard(this);
	}

	/**
	 * 检测是否为简单密码
	 * 
	 * @param pwd
	 * @return
	 */
	private boolean isSimplePwd(String pwd) {
		for (int i = 0; i < pwd.length(); i++) {
			if (i != (pwd.length() - 1)) {
				if (pwd.charAt(i) != pwd.charAt(i + 1))
					return false;
			}
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#initData()
	 */
	@Override
	protected void initData() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#handleCheckEmailEvent(boolean)
	 */
	@Override
	public void handleCheckEmailEvent(boolean isEmailAvailable) {
		// TODO Auto-generated method stub
		super.handleCheckEmailEvent(isEmailAvailable);
		if (isEmailAvailable) {
			// 跳转到填写信息
			Intent i = new Intent(this, EnterPersonInfoActivity.class);
			ALLocalEnv.getLocalEnv().getMyProfile()
					.setEmail(etUsername.getText().toString().trim());
			ALLocalEnv.getLocalEnv().getMyProfile()
					.setPwd(etConfirmPwd.getText().toString().trim());
			startActivity(i);
		} else {
			MyToastUtil.getInstance().showCenterToastOnCenter(
					R.string.TxtEmailUnavailable);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.ui.base.BaseActivity#handleLoginedEvent(com.alstudio.pojo
	 * .ALXmppEvent)
	 */
	@Override
	public void handleLoginedEvent(ALXmppEvent data) {
		// TODO Auto-generated method stub
		ALLog.d("登陆成功,关闭界面");
		finish();
	}

}
